package com.hzlj.position.locate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.base.converter.QueryWrapperConverter;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.dict.annotation.DictConvert;
import com.google.common.collect.Lists;
import com.hzlj.position.config.common.dto.common.LocateTimesDTO;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.JzJbxxDeviceBindQueryResultDTO;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.LocateTimesDeleteDTO;
import com.hzlj.position.config.common.dto.jzJbxxDeviceBind.LocateTimesUpdateDTO;
import com.hzlj.position.config.service.JzJbxxDeviceBindService;
import com.hzlj.position.locate.common.dto.reportInitiativeSettingJzry.ReportInitiativeSettingJzryQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportInitiativeSettingJzry.ReportInitiativeSettingJzryQueryResultDTO;
import com.hzlj.position.locate.common.dto.reportInitiativeSettingJzry.ReportInitiativeSettingJzryUpsertDTO;
import com.hzlj.position.locate.common.model.ReportInitiativeSettingJzry;
import com.hzlj.position.locate.dao.ReportInitiativeSettingJzryDao;
import com.hzlj.position.locate.mapper.ReportInitiativeSettingJzryMapper;
import com.hzlj.position.locate.service.ReportInitiativeSettingJzryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 通信联络核查-人脸签到-配置表-关联矫正人员(ReportInitiativeSettingJzry)表服务接口
 *
 * @author lifh
 * @date 2023-11-10 18:31:54
 */
@Slf4j
@Service
public class ReportInitiativeSettingJzryServiceImpl implements ReportInitiativeSettingJzryService {
    @Resource
    private ReportInitiativeSettingJzryDao reportInitiativeSettingJzryDao;
    @Resource
    private ReportInitiativeSettingJzryMapper reportInitiativeSettingJzryMapper;
    @Resource
    private JzJbxxDeviceBindService jzJbxxDeviceBindService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upsertReportInitiativeSettingJzry(String jzId, List<String> settingIds) {
        if (ObjectUtil.isEmpty(settingIds)) {
            this.deleteReportInitiativeSettingJzry(jzId);
            return;
        }
        this.upsertReportInitiativeSettingJzry(
                settingIds.stream().map(e -> new ReportInitiativeSettingJzryUpsertDTO().setSettingId(e).setJzId(jzId)).collect(Collectors.toList())
        );
    }

    @Override
    public void upsertReportInitiativeSettingJzry(List<ReportInitiativeSettingJzryUpsertDTO> dtos) {
        if (ObjectUtil.isEmpty(dtos)) {
            return;
        }
        List<String> jzIds = dtos.stream().map(ReportInitiativeSettingJzryUpsertDTO::getJzId).distinct().collect(Collectors.toList());
        //先删除
        reportInitiativeSettingJzryDao.deleteReportInitiativeSettingJzry(jzIds);
        //再新增
        List<ReportInitiativeSettingJzry> adds = dtos.stream().map(
                e -> BeanUtil.copyProperties(e, ReportInitiativeSettingJzry.class)
        ).collect(Collectors.toList());
        this.reportInitiativeSettingJzryDao.saveBatch(adds);
    }

    @Override
    public boolean deleteReportInitiativeSettingJzry(String jzId) {
        return reportInitiativeSettingJzryDao.deleteReportInitiativeSettingJzry(
                Lists.newArrayList(jzId)
        );
    }

    @Override
    public Boolean exists(List<String> settingIds) {
        return this.reportInitiativeSettingJzryDao.exists(settingIds);
    }

    @Override
    public void updateLocateTimes(LocateTimesUpdateDTO dto) {
        List<JzJbxxDeviceBindQueryResultDTO> binds = jzJbxxDeviceBindService.updateLocateTimes(dto);//批量更新
        List<ReportInitiativeSettingJzryUpsertDTO> dtos = binds.stream().flatMap(e ->
                e.getLocateTimes().stream().map(ee -> new ReportInitiativeSettingJzryUpsertDTO().setSettingId(ee.getId()).setJzId(e.getJzId())).collect(toList()).stream()
        ).collect(toList());
        this.upsertReportInitiativeSettingJzry(dtos);
    }

    @Override
    public void deleteLocateTimes(LocateTimesDeleteDTO dto) {
        JzJbxxDeviceBindQueryResultDTO update = jzJbxxDeviceBindService.deleteLocateTimes(dto);
        //保存新的
        this.upsertReportInitiativeSettingJzry(
                dto.getJzId(),
                update.getLocateTimes().stream().map(LocateTimesDTO.Item::getId).collect(toList())
        );
    }


    /**
     * 查询列表：分页
     */
    @Override
    @DictConvert
    public QueryPageResultDTO<ReportInitiativeSettingJzryQueryResultDTO> pageReportInitiativeSettingJzry(ReportInitiativeSettingJzryQueryParamDTO dto, PageParamDTO page) {
        return QueryWrapperConverter.fromPage(this.reportInitiativeSettingJzryMapper.page(
                        new Page<>(page.getPage(), page.getSize()), dto
                )
        );
    }
}
